package com.jyb.qiyefuwu.viewmodel;

import android.app.Application;
import android.content.Intent;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.jyb.qiyefuwu.api.EnterpriseServiceAPIManager;
import com.jyb.qiyefuwu.entity.DatabaseListEntiy;
import com.jyb.qiyefuwu.eventmodel.UpdateSaleListEvent;
import com.sgb.net.entity.BaseEntity;
import com.sgb.net.entity.ListResponse;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.customview.PhotoViewActivity;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class DatabaseListModel extends BaseViewModel {
    public MutableLiveData<List<DatabaseListEntiy>> listData = new MutableLiveData<>();
    public MutableLiveData<List<DatabaseListEntiy>> voucherData = new MutableLiveData<>();
    public MutableLiveData<List<DatabaseListEntiy>> billingData = new MutableLiveData<>();
    public MutableLiveData<String> delOk = new MutableLiveData<>();
    public MutableLiveData<String> delFail = new MutableLiveData<>();
    public MutableLiveData<String> commitOk = new MutableLiveData<>();
    public MutableLiveData<String> commitFail = new MutableLiveData<>();
    public MutableLiveData<String> toastMsg = new MutableLiveData<>();
    private int limit = 8;
    private int page = 1;

    public DatabaseListModel(@NonNull Application application) {
        super(application);
    }

    /**
     * 资料库列表
     *
     * @param isBuyers
     * @param isLodMore
     * @param orderId
     */
    public void getDatabaseList(int isBuyers, boolean isLodMore, String orderId) {
        mLoading.postValue(SHOW_LODING);
        page = isLodMore ? page + 1 : 1;
        EnterpriseServiceAPIManager.getInstance().getUserCenterAPI().databaseList(limit, orderId, page, isBuyers).enqueue(new Callback<BaseEntity<ListResponse<List<DatabaseListEntiy>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<DatabaseListEntiy>>>> call, Response<BaseEntity<ListResponse<List<DatabaseListEntiy>>>> response) {
                mLoading.postValue(DISMISS_LODING);
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals("ok")) {
                    listData.postValue(response.body().getData().getList());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<DatabaseListEntiy>>>> call, Throwable t) {
                mLoading.postValue(DISMISS_LODING);
            }
        });
    }

    /**
     * 资料库删除
     * @param id
     */
    public void delDatabase(String id) {
        mLoading.postValue(SHOW_LODING);
        EnterpriseServiceAPIManager.getInstance().getUserCenterAPI().delDatabase(Integer.valueOf(id)).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                mLoading.postValue(DISMISS_LODING);
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals("ok")) {
                    delOk.postValue(response.body().getMsg());
                } else {
                    delFail.postValue(response.body().getMsg());
                }
                toastMsg.postValue(response.body().getMsg());
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                mLoading.postValue(DISMISS_LODING);
            }
        });
    }

    /**
     * 凭证列表
     *
     * @param isLodMore
     * @param orderId
     */
    public void getVoucherList(boolean isLodMore, String orderId) {
        mLoading.postValue(SHOW_LODING);
        page = isLodMore ? page + 1 : 1;
        EnterpriseServiceAPIManager.getInstance().getUserCenterAPI().voucherList(limit, orderId, page).enqueue(new Callback<BaseEntity<ListResponse<List<DatabaseListEntiy>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<DatabaseListEntiy>>>> call, Response<BaseEntity<ListResponse<List<DatabaseListEntiy>>>> response) {
                mLoading.postValue(DISMISS_LODING);
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals("ok")) {
                    voucherData.postValue(response.body().getData().getList());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<DatabaseListEntiy>>>> call, Throwable t) {
                mLoading.postValue(DISMISS_LODING);
            }
        });
    }

    /**
     * 删除凭证
     * @param id
     */
    public void delVoucher(String id) {
        mLoading.postValue(SHOW_LODING);
        EnterpriseServiceAPIManager.getInstance().getUserCenterAPI().delVoucher(Integer.valueOf(id)).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                mLoading.postValue(DISMISS_LODING);
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals("ok")) {
                    delOk.postValue(response.body().getMsg());
                } else {
                    delFail.postValue(response.body().getMsg());
                }
                toastMsg.postValue(response.body().getMsg());
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                mLoading.postValue(DISMISS_LODING);
            }
        });
    }

    /**
     * 提交凭证
     * @param orderId  104开头的id
     */
    public void submitVoucher(String orderId) {
        mLoading.postValue(SHOW_LODING);
        EnterpriseServiceAPIManager.getInstance().getUserCenterAPI().submitVoucher(orderId).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                mLoading.postValue(DISMISS_LODING);
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals("ok")) {
                    commitOk.postValue(response.body().getMsg());
                    EventBus.getDefault().post(new UpdateSaleListEvent(4,0));
                } else {
                    commitFail.postValue(response.body().getMsg());
                }
                toastMsg.postValue(response.body().getMsg());
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                mLoading.postValue(DISMISS_LODING);
            }
        });
    }

    /**
     * 开票列表
     *
     * @param isLodMore
     * @param orderId
     */
    public void getBillingList(boolean isLodMore, String orderId) {
        mLoading.postValue(SHOW_LODING);
        page = isLodMore ? page + 1 : 1;
        EnterpriseServiceAPIManager.getInstance().getUserCenterAPI().billingList(limit, orderId, page).enqueue(new Callback<BaseEntity<ListResponse<List<DatabaseListEntiy>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<DatabaseListEntiy>>>> call, Response<BaseEntity<ListResponse<List<DatabaseListEntiy>>>> response) {
                mLoading.postValue(DISMISS_LODING);
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals("ok")) {
                    billingData.postValue(response.body().getData().getList());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<DatabaseListEntiy>>>> call, Throwable t) {
                mLoading.postValue(DISMISS_LODING);
            }
        });
    }

    /**
     * 删除开票信息
     * @param id
     */
    public void delBilling(String id) {
        mLoading.postValue(SHOW_LODING);
        EnterpriseServiceAPIManager.getInstance().getUserCenterAPI().delBilling(Integer.valueOf(id)).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                mLoading.postValue(DISMISS_LODING);
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals("ok")) {
                    delOk.postValue(response.body().getMsg());
                } else {
                    delFail.postValue(response.body().getMsg());
                }
                toastMsg.postValue(response.body().getMsg());
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                mLoading.postValue(DISMISS_LODING);
            }
        });
    }
}
